<?php
/**
 * Created by PhpStorm.
 * User: summercode
 * Date: 2018/11/15
 * Time: 10:46 PM
 */

namespace app\common\controller;

use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use think\Config;
use think\Controller;

class QiuniUpload extends Controller
{


    public function simpleUpload($local_url, $dir ,$key_url) {
        $accessKey = Config::get('upimg.accessKey');
        $secretKey = Config::get('upimg.secretKey');
        $bucket = Config::get('upimg.bucket');

        $auth = new Auth($accessKey, $secretKey);

        $token = $auth->uploadToken($bucket);

// 要上传文件的本地路径
        $filePath = $local_url;

// 上传到七牛后保存的文件名
        $key =  $dir . DS . $key_url;

// 初始化 UploadManager 对象并进行文件的上传。
        $uploadMgr = new UploadManager();

// 调用 UploadManager 的 putFile 方法进行文件的上传。
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);

        if ($err !== null) {
            return null;
        } else {
            return Config::get('upimg.url') . $dir . DS . $key_url;
        }

    }
}